package net.atomarrow.services;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import net.atomarrow.annotation.Inject;
import net.atomarrow.bean.Pager;
import net.atomarrow.bean.ServiceResult;
import net.atomarrow.configs.Config;
import net.atomarrow.configs.LogConfig;
import net.atomarrow.db.orm.IDomainControl;
import net.atomarrow.db.parser.Conditions;
import net.atomarrow.domains.Domain;
import net.atomarrow.ioc.ActionContext;
import net.atomarrow.util.StringUtil;

/**
 * 服务类
 * @author Michael
 *
 */
public class Service implements IService{

    @Inject
    private JdbcService service;
    private Logger log = Logger.getLogger("Service执行操作:");
    protected ServiceResult SUCCESS=new ServiceResult(true, null, null);
    @Override
    public Serializable add(Domain domain) {
        Serializable id=service.add(domain);
        if(LogConfig.printServiceLog()){
            log.info(getTableName(domain)+" 成功执行一条add语句,主键："+id);
        }
        return id;
    }

    @Override
    public Serializable add(String tableName,Domain domain) {
        Serializable id=service.add(tableName, domain);
        if(LogConfig.printServiceLog()){
            log.info(tableName+" 成功执行一条add语句,主键："+id);
        }
        return id;
    }

    @Override
    public boolean addByBatch(List<? extends Domain> dataList) {
        boolean success= service.addByBatch(dataList);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(dataList.get(0))+" 成功执行插入"+dataList.size()+"条数据");
            }else{
                log.info("批量添加未插入任何数据");
            }
        }
        return success;
    }

    @Override
    public <T extends Domain> boolean addByBatch(Class<T> clazz, List<T> dataList) {
        boolean success=service.addByBatch(clazz, dataList);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(clazz)+" 成功执行插入"+dataList.size()+"条数据");
            }else{
                log.info(" 未插入任何数据");
            }
        }
        return success;
    }

    @Override
    public boolean checkExist(Class clazz, String colName, Serializable value,
            Serializable exceptId) {
        boolean success= service.checkExist(clazz, colName, value, exceptId);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(clazz)+" 查询到存在相同数据");
            }else{
                log.info(getTableName(clazz)+" 未查询到相同数据");
            }
        }
        return success;
    }

    @Override
    public int del(Domain domain) {
        int i=service.del(domain);
        if(LogConfig.printServiceLog()){
            log.info(getTableName(domain)+"执行"+i+"条删除语句");
        }
        return i;
    }

    @Override
    public int delByBatch(Conditions conditions) {
        int count= service.delByBatch(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" 批量删除了"+count+"条数据");
        }
        return count;
    }

    @Override
    public <T> int delById(Class<T> clazz, Serializable id) {
        int count= service.delById(clazz, id);
        if(LogConfig.printServiceLog()){
            log.info(getTableName(clazz)+" 成功删除了"+count+"条数据");
        }
        return count;
    }

    @Override
    public <T> int delById(String className, Serializable id) {
        int count=  service.delById(className, id);
        if(LogConfig.printServiceLog()){
            log.info(className+"成功删除了"+count+"条数据");
        }
        return count;
    }

    @Override
    public int executeUpdate(String Hql, Object[] params) {
        int count= service.executeUpdate(Hql, params);
        if(LogConfig.printServiceLog()){
            log.info(Hql);
            log.info("成功执行了"+count+"条数据");
        }
        return count;
    }

    @Override
    public <T> T getById(Class<T> clazz, Serializable id) {
        T t= service.getById(clazz, id);
        if(LogConfig.printServiceLog()){
            if(t!=null){
                log.info(getTableName(clazz)+" 根据id:"+id+"成功获取一个对象");
            }else{
                log.info(getTableName(clazz)+" 根据id:"+id+"未查询到对象");
            }
        }
        return t;
    }


    @Override
    public <T> List<T> getColumnList(Conditions conditions, String attribute) {
        List<T> ts= service.getColumnList(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getColumnList查询到数量："+ts.size());
        }
        return ts;
    }
    
    @Override
    public <T> List<T> getColumnDistinctList(Conditions conditions, String attribute) {
        List<T> ts= service.getColumnDistinctList(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getColumnDistinctList查询到数量："+ts.size());
        }
        return ts;
    }

    @Override
    public <T> List<T> getColumnListByPage(Conditions conditions,
    		String attribute, Pager pager) {
    	List<T> ts=  service.getColumnListByPage(conditions, attribute, pager);
    	if(LogConfig.printServiceLog()){
    		log.info(conditions.getTableName()+" getColumnListByPage查询到数量："+ts.size());
    	}
    	return ts;
    }
    @Override
    public <T> List<T> getColumnDistinctListByPage(Conditions conditions,
            String attribute, Pager pager) {
        List<T> ts=  service.getColumnDistinctListByPage(conditions, attribute, pager);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getColumnDistinctListByPage查询到数量："+ts.size());
        }
        return ts;
    }

    @Override
    public int getCount(Conditions conditions) {
        int count= service.getCount(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getCount查询到数量："+count);
        }
        return count;
    }

    @Override
    public <T> List<T> getDistinctList(Conditions conditions, String attribute) {
    	List<T> ts= service.getDistinctList(conditions, attribute);
    	if(LogConfig.printServiceLog()){
    		log.info(conditions.getTableName()+" getDistinctList查询到数量："+ts.size());
    	}
    	return ts;
    }
    @Override
    public int getDistinctCount(Conditions conditions, String attribute) {
        int count= service.getDistinctCount(conditions,attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getDistinctCount查询到数量："+count);
        }
        return count;
    }

    
    
    @Override
    public List<Map<String, Object>> getMapList(Conditions conditions) {
    	List<Map<String, Object>> ts=  service.getMapList(conditions);
    	if(LogConfig.printServiceLog()){
    		log.info(conditions.getTableName()+" getMapList查询到数量："+ts.size());
    	}
    	return ts;
    }
    
    @Override
    public <T> List<T> getList(Conditions conditions) {
        List<T> ts=  service.getList(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getList查询到数量："+ts.size());
        }
        return ts;
    }


    @Override
    public <T> List<T> getListByPage(Conditions conditions, Pager pager) {
        List<T> ts=  service.getListByPage(conditions, pager);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getListByPage查询到数量："+ts.size());
        }
        return ts;
    }

    @Override
    public <T> List<T> getListWithColumn(Conditions conditions, String[] columns) {
        List<T> ts= service.getListWithColumn(conditions, columns);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getListWithColumn查询到数量："+ts.size());
        }
        return ts;
    }

    @Override
    public <T> List<T> getListWithColumnByPage(Conditions conditions,
            String[] columns, Pager pager) {
        List<T> ts= service.getListWithColumnByPage(conditions, columns, pager);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getListWithColumnByPage查询到数量："+ts.size());
        }
        return ts;
    }

    @Override
    public Double getMax(Conditions conditions, String attribute) {
        Double max= service.getMax(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" max查询到数值："+max);
        }
        return max;
    }

    @Override
    public <T> T getMaxDomain(Conditions conditions, String attribute) {
        T t= service.getMaxDomain(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getMaxDomain查询到数值："+t);
        }
        return t;
    }


    @Override
    public <T> T getOne(Conditions conditions) {
        T t= service.getOne(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getOne查询到内容："+t);
        }
        return t;
    }


    @Override
    public double getSum(Conditions conditions, String attribute) {
        double sum= service.getSum(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getSum查询到内容："+sum);
        }
        return sum;
    }

    @Override
    public int modify(Domain domain) {
        int i=service.modify(domain);
        if(LogConfig.printServiceLog()){
            log.info(getTableName(domain)+" 执行"+i+"条修改语句");
        }
        return i;
    }


    @Override
    public boolean modifyByBatch(List<? extends Domain> dataList) {
        boolean success=service.modifyByBatch(dataList);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(dataList.get(0))+" 批量更新条目："+dataList.size());
            }else{
                log.info("未批量更新任何数据");
            }
        }
        return success;
    }

    @Override
    public boolean modifyByBatch(String tableName, List<? extends Domain> dataList) {
        boolean success= service.modifyByBatch(tableName, dataList);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(tableName+" 批量更新条目："+dataList.size());
            }else{
                log.info(tableName+" 未批量更新任何数据");
            }
        }
        return success;
    }

    @Override
    public int modifyWithColumn(Conditions conditions,
            Map<String, Serializable> columns) {
        int count= service.modifyWithColumn(conditions, columns);
        if(LogConfig.printServiceLog()){
                log.info(conditions.getTableName()+" modifyWithColumn：执行条数"+count);
        }
        return count;
    }

    @Override
    public int modifyWithColumn(Conditions conditions, Serializable[] keyValues) {
        int count = service.modifyWithColumn(conditions, keyValues);
        if (LogConfig.printServiceLog()) {
            log.info(conditions.getTableName() + "modifyWithColumn：执行条数"+ count);
        }
        return count;

    }

    @Override
    public boolean saveOrUpdate(List<? extends Domain> datas) {
        boolean success=service.saveOrUpdate(datas);
        if (LogConfig.printServiceLog()) {
        	if(success){
        		log.info(getTableName(datas.get(0))+"saveOrUpdate执行条数："+datas.size());
        	}else{
        		log.info("saveOrUpdate未更新任何数据");
        	}
        }
        return success;
    }

    @Override
    public void saveOrUpdate(Domain data) {
        service.saveOrUpdate(data);
        if (LogConfig.printServiceLog()) {
            log.info(getTableName(data)+"saveOrUpdate执行");
        }
    }

    @Override
    public  boolean addByBatch(String tableName, List<? extends Domain> dataList) {
        boolean success= service.addByBatch(tableName, dataList);
        if (LogConfig.printServiceLog()) {
            if(success){
                log.info(tableName+" addByBatch添加条数："+dataList.size());
            }else{
                log.info(tableName+" addByBatch未插入任何数据");
            }
        }
        return success;
    }

    @Override
    public int modify(String tableName,Domain domain) {
        int i = service.modify(tableName,domain);
        if (LogConfig.printServiceLog()) {
            log.info(tableName+" 更新执行"+i+"条语句");
        }
        return i;
    }

    @Override
    public <T> T getById(String tableName, Serializable id) {
        T t= service.getById(tableName, id);
        if (LogConfig.printServiceLog()) {
            log.info(tableName+" 根据id："+id+"取得内容"+t);
        }
        return t;
    }

    @Override
    public boolean checkExist(String tableName, String colName, Serializable value,
            Serializable exceptId) {
        boolean success=service.checkExist(tableName, colName, value, exceptId);
        if (LogConfig.printServiceLog()) {
            log.info(tableName+" checkExist:"+success);
        }
        return success;
    }

    @Override
    public boolean saveOrUpdate(String tableName, List<? extends Domain> datas) {
        boolean success=service.saveOrUpdate(tableName, datas);
        if (LogConfig.printServiceLog()) {
            if(success){
                log.info(tableName+" saveOrUpdate语句执行条数"+datas.size());
            }else{
                log.info(tableName+" saveOrUpdate语句未更新任何数据");
            }
        }
        return success;
    }

    @Override
    public void saveOrUpdate(String tableName, Domain data) {
        service.saveOrUpdate(tableName, data);
        if (LogConfig.printServiceLog()) {
            log.info(tableName+" saveOrUpdate语句执行");
        }
    }

    @Override
    public void execute(String sql) {
        service.execute(sql);
        if (LogConfig.printServiceLog()) {
            log.info("execute语句执行:"+sql);
        }
    }

    @Override
    public boolean modifyByBatchWithColumn(List<? extends Domain> dataList, String[] cols) {
        boolean success=service.modifyByBatchWithColumn(dataList,cols);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(dataList.get(0))+" 批量更新指定列条目："+dataList.size());
            }else{
                log.info("未批量更新任何数据");
            }
        }
        return success;
    }

    @Override
    public boolean modifyByBatchWithColumn(String tableName, List<? extends Domain> dataList,
            String[] cols) {
        boolean success=service.modifyByBatchWithColumn(tableName, dataList, cols);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(tableName+" 批量更新指定列条目："+dataList.size());
            }else{
                log.info("未批量更新任何数据");
            }
        }
        return success;
        
    }

    @Override
    public String getGroupConcat(Conditions conditions, String attribute,
            String separator) {
        
        String result=service.getGroupConcat(conditions, attribute, separator);
        if(LogConfig.printServiceLog()){
            if(StringUtil.isNotBlank(result)){
                log.info(conditions.getTableName()+" GroupContact查询到内容合并信息："+result);
            }else{
                log.info(conditions.getTableName()+"GroupContact 未查询到任何信息");
            }
        }
        return result;
    }


    protected ServiceResult success(Object obj){
        return success(obj,null);
    }
    protected ServiceResult successWith(Object obj,String... param){
        return successWith(obj,null,param);
    }
    protected ServiceResult successWithout(Object obj,String... param){
        return successWithout(obj,null,param);
    }
    protected ServiceResult success(Object obj,String msg){
        return new ServiceResult(true,obj,msg);
    }
    protected ServiceResult successWith(Object obj,String msg,String... param){
        return new ServiceResult(true,obj,msg,param,null);
    }
    protected ServiceResult successWithout(Object obj,String msg,String... param){
        return new ServiceResult(true,obj,msg,null,param);
    }
    protected ServiceResult error(String msg){
        if(LogConfig.printServiceLog()){
            log.info(msg);
        }
        ActionContext.getContext().setNeedRollback(true);
        return new ServiceResult(false,null,msg);
    }   
    protected ServiceResult warn(String msg){
        if(LogConfig.printServiceLog()){
            log.info(msg);
        }
        return new ServiceResult(true,true,null,msg);
    }

    @Override
    public Double getDouble(Conditions conditions) {
        Double number= service.getDouble(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getDouble查询到数值："+number);
        }
        return number;
    }

    @Override
    public Integer getInteger(Conditions conditions) {
        Integer number= service.getInteger(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getInteger查询到数值："+number);
        }
        return number;
    }

    @Override
    public Long getLong(Conditions conditions) {
        Long number= service.getLong(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getLong查询到数值："+number);
        }
        return number;
    }

    @Override
    public Date getDate(Conditions conditions) {
        Date date= service.getDate(conditions);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getDate查询到数值："+date);
        }
        return date;
    }

    @Override
    public Double getMin(Conditions conditions, String attribute) {
        Double number= service.getMin(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getMin查询到数值："+number);
        }
        return number;
    }

    @Override
    public <T> T getMinDomain(Conditions conditions, String attribute) {
        T t= service.getMinDomain(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getMinDomain查询到数值："+t);
        }
        return t;
    }

    @Override
    public <T> T getColumn(Conditions conditions, String attribute) {
        T t= service.getColumn(conditions, attribute);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getColumn查询到内容："+t);
        }
        return t;
    }

    @Override
    public <T> List<T> getDistinctListPage(Conditions conditions,
            String attribute, Pager pager) {
        List<T> ts= service.getDistinctListPage(conditions, attribute,pager);
        if(LogConfig.printServiceLog()){
            log.info(conditions.getTableName()+" getDistinctListPage查询到数量："+ts.size());
        }
        return ts;
    }

	@Override
	public boolean checkExist(Class clazz, String colName, Serializable value) {
		return checkExist(clazz, colName, value, 0);
	}

	@Override
	public boolean checkExist(String tableName, String colName,
			Serializable value) {
		return checkExist(tableName, colName, value, 0);
	}


	@Override
	public boolean checkMultiExist(String tableName, Serializable... args) {
		boolean success= service.checkMultiExist(tableName, args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(tableName+" 查询到存在相同数据");
            }else{
                log.info(tableName+" 未查询到相同数据");
            }
        }
        return success;
	}

	@Override
	public boolean checkMultiExistWithExceptId(String tableName,
			Serializable exceptId, Serializable... args) {
		boolean success= service.checkMultiExistWithExceptId(tableName, exceptId, args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(tableName+" 查询到存在相同数据");
            }else{
                log.info(tableName+" 未查询到相同数据");
            }
        }
        return success;
	}

	@Override
	public boolean checkMultiExist(Class clazz, Serializable... args) {
		boolean success= service.checkMultiExist(clazz, args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(clazz)+" 查询到存在相同数据");
            }else{
                log.info(getTableName(clazz)+" 未查询到相同数据");
            }
        }
        return success;
	}

	@Override
	public boolean checkMultiExistWithExceptId(Class clazz,
			Serializable exceptId, Serializable... args) {
		boolean success= service.checkMultiExistWithExceptId(clazz, exceptId, args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(clazz)+" 查询到存在相同数据");
            }else{
                log.info(getTableName(clazz)+" 未查询到相同数据");
            }
        }
        return success;
	}
	/**
	 * 获得domainControl,没有则返回null
	 * @return
	 */
	private IDomainControl getDomainControl(){
		return Config.getConfig().configDomainControl();
	}

	/**
	 * 获得表名字
	 * @param domain
	 * @return
	 */
	private String getTableName(Domain domain){
		String tableName;
		if(getDomainControl()!=null){
			tableName=getDomainControl().getDomainName(domain.getClass());
		}else{
			tableName=domain.getClass().getSimpleName();
		}
		return tableName;
	}
	/**
	 * 获得表名字
	 * @param domain
	 * @return
	 */
	private String getTableName(Class clazz){
		String tableName;
		if(getDomainControl()!=null){
			tableName=getDomainControl().getDomainName(clazz);
		}else{
			tableName=clazz.getSimpleName();
		}
		return tableName;
	}

	@Override
	public boolean checkMultiExistByOr(Class clazz, Serializable... args) {
		boolean success= service.checkMultiExistByOr(clazz, args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(clazz)+" 查询到存在相同数据");
            }else{
                log.info(getTableName(clazz)+" 未查询到相同数据");
            }
        }
        return success;
	}

	@Override
	public boolean checkMultiExistWithExceptIdByOr(Class clazz,
			Serializable exceptId, Serializable... args) {
		boolean success= service.checkMultiExistWithExceptIdByOr(clazz,exceptId, args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(getTableName(clazz)+" 查询到存在相同数据");
            }else{
                log.info(getTableName(clazz)+" 未查询到相同数据");
            }
        }
        return success;
	}

	@Override
	public boolean checkMultiExistByOr(String tableName, Serializable... args) {
		boolean success= service.checkMultiExistByOr(tableName,args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(tableName+" 查询到存在相同数据");
            }else{
                log.info(tableName+" 未查询到相同数据");
            }
        }
        return success;
	}

	@Override
	public boolean checkMultiExistWithExceptIdByOr(String tableName,
			Serializable exceptId, Serializable... args) {
		boolean success= service.checkMultiExistWithExceptIdByOr(tableName, exceptId, args);
        if(LogConfig.printServiceLog()){
            if(success){
                log.info(tableName+" 查询到存在相同数据");
            }else{
                log.info(tableName+" 未查询到相同数据");
            }
        }
        return success;
	}

}
